package cn.team2.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * 使用 RestTemplate 调用http接口查天气
 */
@Slf4j
@RestController
@RequestMapping("/v1")
public class WeatherController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/getwather")
    public String getWeather(String cityName) {
        log.info("cn.team2.controller.WeatherController#getwather cityName:{}", cityName);
        String url = "http://autodev.openspeech.cn/csp/api/v2.1/weather?openId=aiuicus&clientType=android&sign=android&city=" + cityName + "&needMoreData=true&pageNo=1&pageSize=7";
        log.info("cn.team2.controller.WeatherController#getwather url:{}", url);
        String string = restTemplate.getForObject(url, String.class);
        log.info("cn.team2.controller.WeatherController#getwather string:{}", string);
        return string;
    }
    @GetMapping("/getwather/{cityCode}")
    public String getWeather2(@PathVariable("cityCode") String cityCode) {
        log.info("cn.team2.controller.WeatherController#getWeather2 cityCode:{}", cityCode);
        String url = "http://t.weather.sojson.com/api/weather/city/"+cityCode;
        log.info("cn.team2.controller.WeatherController#getWeather2 url:{}", url);
        String string = restTemplate.getForObject(url, String.class);
        log.info("cn.team2.controller.WeatherController#getWeather2 result:{}", string);
        return string;
    }
}